from collections import deque

n, x1, y1, x2, y2 = map(int, input().split())


# for next_node in [(nx+1,ny+2),(nx+1,ny-2),(nx-1,ny+2),(nx-1,ny+2),(nx+2,ny+1),(nx+2,ny-1),(nx-2,ny+1),(nx-2,ny-1)]:
# for next_node in [(nx+2,ny+2),(nx+2,ny-2),(nx-2,ny+2),(nx-2,ny-2)]:

def main():
    start_q = deque([(x1, y1)])
    target_q = deque([(x2, y2)])
    start_visited = {(x1, y1): 0}
    target_visited = {(x2, y2): 0}
    min_cost = float('inf')
    while start_q or target_q:
        # 从开始搜一下 从结束搜一下
        if start_q:
            curr = start_q.popleft()
            nx, ny = curr
            if curr in target_visited:
                min_cost = min(target_visited[curr] + start_visited[curr], min_cost)
            for next_node in [(nx + 1, ny + 2), (nx + 1, ny - 2), (nx - 1, ny + 2), (nx - 1, ny + 2), (nx + 2, ny + 1),
                              (nx + 2, ny - 1), (nx - 2, ny + 1), (nx - 2, ny - 1)]:
                dx, dy = next_node
                if dx < 0 or dx > n: continue
                if dy < 0 or dy > n: continue
                if next_node in start_visited: continue
                start_visited[next_node] = start_visited[curr] + 1
                start_q.append(next_node)
        if target_q:
            curr = target_q.popleft()
            nx, ny = curr
            if curr in start_visited:
                min_cost = min(target_visited[curr] + start_visited[curr], min_cost)
            for next_node in [(nx + 2, ny + 2), (nx + 2, ny - 2), (nx - 2, ny + 2), (nx - 2, ny - 2)]:
                dx, dy = next_node
                if dx < 0 or dx > n: continue
                if dy < 0 or dy > n: continue
                if next_node in target_visited: continue
                target_visited[next_node] = target_visited[curr] + 1
                target_q.append(next_node)
    if min_cost == float('inf'): return -1
    return min_cost


if __name__ == '__main__':
    print(main())
